/*
 * Copyright 2009 Jiwei Xu (http://www.xujiwei.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

provide("Objects.ImageObj");

require("Objects.FigureObj");

ImageObj = Class(FigureObj, {
    Initialize : function(image, pt, size) {
        this.Base();

        var x = pt.X, y = pt.Y;
        var w = image.width, h = image.height;
        this.Handles = [
            { X : x,        Y : y },
            { X : x + w/2,  Y : y },
            { X : x + w,    Y : y },
            { X : x + w,    Y : y + h/2 },
            { X : x + w,    Y : y + h },
            { X : x + w/2,  Y : y + h },
            { X : x,        Y : y + h },
            { X : x,        Y : y + h/2 }
        ];

        this.Image = image;
        this.Width = image.width;
        this.Height = image.height;
    },

    Draw : function(ctx) {
        ctx.beginPath();
        ctx.drawImage(this.Image, this.Bounds.Left, this.Bounds.Top, this.Bounds.Width, this.Bounds.Height);
    },

    AdjustHandle : function(index, related, x, y) {
        var handles = this.Handles,
            handle = handles[index];
        for (var i = 0; i < related.length; ++i) {
            if (x) {
                handles[related[i]].X = handle.X;
            }
            if (y) {
                handles[related[i]].Y = handle.Y;
            }
        }
    },

    AdjustMidHandle : function(index) {
        var handles = this.Handles;
        handles[3].Y = handles[7].Y = (handles[0].Y + handles[6].Y) / 2;
        handles[1].X = handles[5].X = (handles[0].X + handles[2].X) / 2;
    },

    MoveHandle : function(index, delta) {
        var h = this.Handles;
        if (h[index]) {
            if (index != 1 && index != 5) {
                h[index].X += delta.X;
            }
            if (index != 3 && index != 7) {
                h[index].Y += delta.Y;
            }
            switch (index) {
                case 0:
                    this.AdjustHandle(index, [1, 2], false, true);
                    this.AdjustHandle(index, [6, 7], true, false);
                    break;
                case 1:
                    this.AdjustHandle(index, [0, 2], false, true);
                    break;
                case 2:
                    this.AdjustHandle(index, [0, 1], false, true);
                    this.AdjustHandle(index, [3, 4], true, false);
                    break;
                case 3:
                    this.AdjustHandle(index, [2, 4], true, false);
                    break;
                case 4:
                    this.AdjustHandle(index, [2, 3], true, false);
                    this.AdjustHandle(index, [5, 6], false, true);
                    break;
                case 5:
                    this.AdjustHandle(index, [4, 6], false, true);
                    break;
                case 6:
                    this.AdjustHandle(index, [4, 5], false, true);
                    this.AdjustHandle(index, [7, 0], true, false);
                    break;
                case 7:
                    this.AdjustHandle(index, [0, 6], true, false);
                    break;
                default:
                    break;
            }
            this.AdjustMidHandle(index);

            this.CalcBounds();
        }
    }
});